package cn.ycl.system.other.config.scheduler.printscheduler;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Author YuanChangLiang
 * @Date 2020/9/22
 * @Desc 运行此类的main方法，即可开始定时执行
 */
public class PrintScheduler {
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        //1.创建调度器
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = stdSchedulerFactory.getScheduler();

        //2.创建JobDetail实例，并与PrintJob类绑定
        JobDetail build = JobBuilder.newJob(PrintJob.class).withIdentity("printJob", "printGroup").build();

        //3.构建Trigger实例,每隔3s执行一次
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("printTrigger", "printTriggerGroup")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(6).repeatForever()).build();

        //4.执行
        scheduler.scheduleJob(build, trigger);
        scheduler.start();

        //睡眠
        TimeUnit.MINUTES.sleep(1);
        scheduler.shutdown();
    }

}
